package com.knight.hadoop.day05.simulate.hdf;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * 上传文件和下载文件 上传文件时，如果文件大小大于一定数量时，将文件传输到多个服务器中。
 *
 */
public class Client {
	
	/**
	 * 缓冲区大小
	 */
	private static final int BUFFER_LENGTH = 1024*10;
	
	/**
	 * 源文件路径
	 */
	private static final String FILE_PATH = "E://README-builds.html";
	
	public static void upload() throws IOException{
		Socket socket = new Socket("localhost", 10000);
		OutputStream out =  socket.getOutputStream();
		
		InputStream in = new FileInputStream(FILE_PATH);
		
		byte[] buffer = new byte[BUFFER_LENGTH];// 缓冲区文件大小为1M
		int len = 0;
		while ((len = in.read(buffer, 0, BUFFER_LENGTH)) != -1) {
			out.write(buffer, 0, len);
		}
		in.close();
		out.close();
		socket.close();
	}
	
	public static void download(String savePath) throws IOException{
		
		Socket socket = new Socket("localhost", 10000);
		InputStream socketIn =  socket.getInputStream();
		
		OutputStream fileOut = new FileOutputStream(savePath);
		
		byte[] buffer = new byte[BUFFER_LENGTH];// 缓冲区文件大小为1M
		int len = 0;
		while ((len = socketIn.read(buffer, 0, BUFFER_LENGTH)) != -1) {
			fileOut.write(buffer, 0, len);
		}
		fileOut.close();
		socketIn.close();
		socket.close();
		
	}
	
	

	public static void main(String[] args) throws IOException {
		download("D://a.html");
	}
}
